package com.rc.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rc.activity.dao.tbl_Category;
import com.rc.activity.dao.tbl_Item;
import com.rc.activity.dao.tbl_Trip;
import com.rc.db.DatabaseSupport;
import com.rc.util.Util;
import com.telpoo.frame.ui.BaseActivity;

public class ReceiptBoxDetailEditItem extends BaseActivity implements
		OnClickListener {

	private LinearLayout llSave, llCancel;
	private tbl_Item objItem;
	private TextView txtItemName, txtAmount, txtMemo, txtCategory, txtTripName,
			lbCategory;
	private tbl_Category objCategory;
	private tbl_Trip objTrip;

	public static void callIntent(Context ct, tbl_Item objItem) {
		Intent it = new Intent(ct, ReceiptBoxDetailEditItem.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("PUT_ITEM", objItem);
		it.putExtras(bundle);
		ct.startActivity(it);
	}

	public static void callIntentReportDetail(Context ct, tbl_Item objItem,
			String reportType) {
		Intent it = new Intent(ct, ReceiptBoxDetailEditItem.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("PUT_ITEM", objItem);
		it.putExtras(bundle);
		it.putExtra("REPORT_TYPE", reportType);
		ct.startActivity(it);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.receiptdetail_edititem);

		llCancel = (LinearLayout) findViewById(R.id.llCancel);
		llSave = (LinearLayout) findViewById(R.id.llSave);
		txtTripName = (TextView) findViewById(R.id.txtTripName);
		txtAmount = (TextView) findViewById(R.id.txtAmount);
		txtItemName = (TextView) findViewById(R.id.txtItemName);
		txtMemo = (TextView) findViewById(R.id.txtMemo);
		txtTripName = (TextView) findViewById(R.id.txtTripName);
		txtCategory = (TextView) findViewById(R.id.txtCategory);
		lbCategory = (TextView) findViewById(R.id.lbCategory);

		txtCategory.setOnClickListener(this);
		txtTripName.setOnClickListener(this);
		llCancel.setOnClickListener(this);
		llSave.setOnClickListener(this);

		// Function
		getDataIntent();

		// set text for txtCategory
		if (objItem.getCategoryID() != 0) {
			objCategory = new tbl_Category();
			objCategory = DatabaseSupport.getCategoryByID(
					objItem.getCategoryID(), ReceiptBoxDetailEditItem.this);
			if (objCategory != null) {
				txtCategory.setText(objCategory.getName());
			}

		}

		// set text for txtTripName
		if (objItem.getTripID() != 0) {
			objTrip = new tbl_Trip();
			objTrip = DatabaseSupport.getTripByID(objItem.getTripID(),
					ReceiptBoxDetailEditItem.this);
			if (objTrip != null) {
				txtTripName.setText(objTrip.getName());
			}

		}

		if (!getString(R.string.choose_trip).equals(
				txtTripName.getText().toString())) {
			lbCategory.setVisibility(View.VISIBLE);
			txtCategory.setVisibility(View.VISIBLE);
		} else {
			lbCategory.setVisibility(View.GONE);
			txtCategory.setVisibility(View.GONE);
		}
		txtItemName.setText(objItem.getName());
		txtAmount.setText(objItem.getAmount() + "");
		txtMemo.setText(objItem.getMemo());
		showAndHiddenContent(DatabaseSupport.check_editItem(
				objItem.getItemID(), ReceiptBoxDetailEditItem.this, model));
	}

	/**
	 * show and hidden content
	 */
	private void showAndHiddenContent(boolean isShow) {
		txtAmount.setEnabled(isShow);
		txtCategory.setEnabled(isShow);
		txtItemName.setEnabled(isShow);
		txtMemo.setEnabled(isShow);
		txtTripName.setEnabled(isShow);
		if (isShow) {
			llSave.setVisibility(View.VISIBLE);

		} else {
			llSave.setVisibility(View.GONE);
		}
	}

	/**
	 * Lấy Object Item từ Intent
	 */

	private void getDataIntent() {
		objItem = new tbl_Item();
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			objItem = (tbl_Item) bundle.getSerializable("PUT_ITEM");
		}
		if (getIntent().hasExtra("REPORT_TYPE")) {
			String mReportType = getIntent().getStringExtra("REPORT_TYPE");
			if (getString(R.string.pending_reports).equals(mReportType)
					|| getString(R.string.submitted_reports)
							.equals(mReportType)) {
				disableLayout();
			}
		}
	}

	/**
	 * Update tbl_Item
	 */
	private int updateData() {
		objItem.setAmount(txtAmount.getText().toString());
		objItem.setName(txtItemName.getText().toString());
		objItem.setMemo(txtMemo.getText().toString());
		objItem.setIsUpdate("1");
		if (objCategory != null) {
			objItem.setCategoryID(objCategory.getCategoryID());
		}

		if (objTrip != null) {
			objItem.setTripID(objTrip.getTripID());
		}
		return DatabaseSupport.updateItem(objItem,
				ReceiptBoxDetailEditItem.this);
	}

	/**
	 * validate UI
	 */

	private boolean validateUI() {
		if (!Util.validateText(txtItemName)) {
			Util.setError((EditText) txtItemName,
					getString(R.string.input_item_name));
		} else if (!Util.validateText(txtAmount)) {
			Util.setError((EditText) txtAmount,
					getString(R.string.input_item_amount));
		} else if (getString(R.string.choose_category).equals(
				txtCategory.getText().toString())
				&& !getString(R.string.choose_trip).equals(
						txtTripName.getText().toString())) {
			showToast(getString(R.string.choose_category));
			CategorizeItems.callIntent(ReceiptBoxDetailEditItem.this);
		} else {
			return true;
		}
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 1) {
			if (data.getExtras().containsKey("CATEGORY_ID")) {
				long mCategoryID = data.getLongExtra("CATEGORY_ID", 0);
				objCategory = new tbl_Category();
				objCategory = DatabaseSupport
						.getCategoryByID(mCategoryID, this);
				txtCategory.setText(objCategory.getName());
			}
		} else if (resultCode == 2) {
			if (data.getExtras().containsKey("TRIP_ID")) {
				long mTripID = data.getLongExtra("TRIP_ID", 0);
				objTrip = new tbl_Trip();
				objTrip = DatabaseSupport.getTripByID(mTripID, this);
				txtTripName.setText(objTrip.getName());
				txtCategory.setVisibility(View.VISIBLE);
				lbCategory.setVisibility(View.VISIBLE);
			}
		}

	}

	/**
	 * Disable all control on view
	 */

	private void disableLayout() {
		llSave.setVisibility(View.GONE);
		txtCategory.setEnabled(false);
		txtTripName.setEnabled(false);
		txtAmount.setEnabled(false);
		txtItemName.setEnabled(false);
		txtMemo.setEnabled(false);
		txtTripName.setEnabled(false);
		txtCategory.setEnabled(false);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.llSave:
			if (validateUI()) {
				if (updateData() > 0) {
					finish();
				}
			}
			break;
		case R.id.llCancel:
			finish();
			break;
		case R.id.txtCategory:
			CategorizeItems.callIntent(this);
			break;
		case R.id.txtTripName:
			TripList.callIntent(this);
			break;
		default:
			break;
		}
	}

	@Override
	public void onSuccess(int taskType, ArrayList<?> list, String msg) {
		super.onSuccess(taskType, list, msg);
		showAndHiddenContent(true);
	}

	@Override
	public void onFail(int taskType, String msg) {
		super.onFail(taskType, msg);
		showAndHiddenContent(false);
	}

}
